<?php

class M_finance extends CI_Model{

	/*==========================Fee Creator============================== */
	public function get_all_fee(){
		$query = $this->db->select(array('fee_id','fee_name','fee_rate'))->get('fee');
		return $query->result();
	}
	
	public function add_fee($fee_name){
		$this->db->set($fee_name)->insert('fee', $fee_name);
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function delete_fee($fee_id){
		$this->db->where('fee_id',$fee_id)->delete(array('fee','fee_table'));
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function get_fee($fee_id){
		$this->db->where('fee_id', $fee_id);
		$query = $this->db->get('fee');
		return $query->result();
	}
	
	public function edit_fee($data,$id){
		 $this->db->where('fee_id',$id)->update('fee',$data);
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	/* year level fees */
	
	public function get_year_level_fees()
	{
		$query = $this->db->get('year_level_fees');
		return $query->num_rows() > 0 ? $query->result() : FALSE;
	}
	
	
	/* fee categories*/
	
	public function get_fee_by_category($id){
		$query = $this->db->select(array('f.fee_name','f.fee_id','f.fee_rate'))
						  ->from(array('fee f','fee_categories fc','fee_table ft'))
						  ->where('fc.fee_cat_id = ft.fee_cat_id')->where('f.fee_id = ft.fee_id')->where('fc.fee_cat_id',$id)->where('ft.fee_cat_id',$id)
						  ->get();
		return $query->num_rows() > 0 ? $query->result() : FALSE;
	}
	
	public function get_fee_category($id=FALSE){
		if($id == FALSE)
			$query = $this->db->select(array('fee_cat_id','fee_cat_name','fee_cat_desc'))->get('fee_categories');
		else
			$query = $this->db->select(array('fee_cat_id','fee_cat_name','fee_cat_desc'))->where('fee_cat_id',$id)->get('fee_categories');
		return $query->num_rows() > 0 ? $query->result() : FALSE;
	}
	
	public function add_fee_category($category){
		$this->db->set($category)->insert('fee_categories',$category);
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function edit_fee_cat($data=FALSE,$cat_id){
		$this->db->set($data)->set('fee_cat_id',$cat_id)->insert('fee_table');
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	// edit category name (description)
	public function edit_fee_cat_prof($cat_id,$cat_name,$cat_desc){
		$this->db->set('fee_cat_name','')->set('fee_cat_desc','')->where('fee_cat_id',$cat_id)->update('fee_categories');
		$this->db->set('fee_cat_name',$cat_name)
				 ->set('fee_cat_desc',$cat_desc)
				 ->set('fee_cat_updated_at','NOW()')
				 ->where('fee_cat_id',$cat_id)->update('fee_categories');
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	// delete from fee_category and fee_table
	public function free_fee_cat($cat_id='', $del_cat= FALSE){
		if($del_cat == FALSE AND $cat_id !== ''){
			$this->db->where('fee_cat_id',$cat_id)->delete('fee_table');
			return $this->db->affected_rows() > 0 ? TRUE : FALSE;
		}elseif($del_cat == TRUE AND $cat_id !== ''){
			$this->db->where('fee_cat_id',$cat_id)->delete(array('fee_categories','fee_table'));
			return $this->db->affected_rows() > 0 ? TRUE : FALSE;
		}else{
			return FALSE;
		}
	}
	
	
	/* end fee category*/
	/*===========================End Fee Creator============================= */
	
	
	
	

}

?>